﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Repast
{
    public partial class Frm_AddMaterial : BaseXtraForm
    {
        //物料分类/物料ID
        public Guid ID { get; set; }

        public event EventHandler SaveEvent;

        public Frm_AddMaterial(EStatus status,Guid id)
        {
            InitializeComponent();
            this.FStatus = status;
            this.ID = id;
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddMaterial_Load(object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            UnitTree.Properties.DataSource = rservice.GetAllUnit();
            SupplierTree.Properties.DataSource = rservice.GetAllSuppliers();
            Material material = null; 

            if (this.FStatus == EStatus.Add)
            {
                this.Text = "物料 - 添加";
            }
            else if (this.FStatus == EStatus.Modify)
            {
                material = rservice.GetMaterialByID(this.ID);
                this.CodeEdit.Text = material.Code;
                this.NameEdit.Text = material.Name;
                this.UnitTree.EditValue = material.Unit.ID;  //或者直接material.UnitID
                this.PlaceEdit.Text = material.Place;
                this.SupplierTree.EditValue = material.SupplierID;

                this.Text = "物料 - 编辑";
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            Material material = null;

            if (this.FStatus == EStatus.Add)
            {
                rservice.AddMaterial(this.CodeEdit.Text.Trim(),this.NameEdit.Text.Trim(),(Guid)this.UnitTree.EditValue,this.PlaceEdit.Text,(Guid)this.SupplierTree.EditValue,this.ID);
                ShowInfo("添加成功!");
            }
            else if (this.FStatus == EStatus.Modify)
            {
                material = rservice.UpdateMaterial(this.CodeEdit.Text.Trim(), this.NameEdit.Text.Trim(), (Guid)this.UnitTree.EditValue, this.PlaceEdit.Text.Trim(),(Guid)this.SupplierTree.EditValue, this.ID);
                ShowInfo("编辑成功!");
            }

            if (SaveEvent != null)
            {
                SaveEvent(material, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
      
    }
}
